// 上传照片列表
<template>
    <div class="picList">
        <v-accordion :title="title" class="infoList" :opened="opened">
            <div slot="title">
                <span v-if="$slots.title"><slot name="title"></slot></span>
                <span v-else>{{title}} <span style="color:#1990FF;font-weight:400">（{{count}}）</span> </span>
            </div>
            <f7-list media-list class="search-list" v-if="list.length > 0">
                <li
                    v-for="(item, index) in list"
                    :key="index"
                    class="content-box"
                >
                    <div class="index-box">{{ index + 1 }}</div>
                    <div class="right-box">
                        <div
                            class="right-box-top"
                            :class="item.allowNone == 1 ? 'checks-box' : ''"
                        >
                            <div class="code">
                                <section>
                                    <span
                                        style="color: #f15a5a; margin-right: 2px"
                                        v-if="item.isMustUpload"
                                    >*</span>
                                    {{ item[props.name] }}
                                    <span v-if="disabled&&item.attachments.length==0" class="noUpload">未上传</span>
                                </section>
                                <div><slot name="nameRight" :bizType="item.bizType"></slot></div>
                            </div>
                            <van-checkbox
                                v-if="item.allowNone == 1"
                                v-model="item.isNil"
                                shape="square"
                                :disabled="disabled"
                                @click="checkItem(index,item)"
                                >无此项</van-checkbox
                            >
                        </div>
                        <div class="right-box-bottom">
                            <qm-file-upload
                                :ref="`${listName}${index + 1}`"
                                :galleryOptions="{ multiple: true, maximum: 9 }"
                                isDefaultFile
                                :defaultFiles="item.attachments"
                                :disabled="disabled"
                                :extensions="extensions"
                                :accept="accept"
                                :actionsOneGroup="actionsGroup(item)"
                                :maximagescount="maximagescount"
                                :showLegend="{show:item.templateAttachs!=null&&item.templateAttachs.length,id:item.templateAttachs!=null&&item.templateAttachs.length ? item.templateAttachs[0].attachmentId : ''}"
                                :params="{
                                    type: 'attach',
                                    dataKey: dataKey,
                                    bizType: item.bizType,
                                    typeCode,
                                    address
                                }"
                                :showOffline="isOffline==1"
                                @onSuccess="fileOnSuccess"
                                @deleteFileAfter="deleteFileAfter"
                                :uploadId="`${listName}${index + 1}`"
                            >
                            </qm-file-upload>
                        </div>
                    </div>
                </li>
            </f7-list>
        </v-accordion>
    </div>
</template>

<script>
import Config from '@/libs/config.js'
export default {
    name: 'v-picture-list',
    props: {
        listName: {
            type: String,
        },
        title: {
            type: String,
        },
        typeCode: {
            type: String,
            require: true,
        },
        props: {
            type: Object,
            default: () => {
                return { name: 'name' }
            },
        },
        // 附件主键
        dataKey: {
            type: String,
            require: true,
        },
        attachCraneType: {
            type: String,
            default: '',
        },
        disabled: {
            type: Boolean,
            default: false,
        },
        extensions: {
            type: String,
            default: 'jpg,jpeg,png',
        },
        accept: {
            type: String,
            default: '.jpg,.jpeg,.png',
        },
        opened: {
            type: Boolean,
            default: true,
        },
        address: {
            type: String,
            default: '',
        },
        // 上传方式的选择 1相册 2拍照 3文件
        actionsOneGroup: {
            type: Array,
            default: () => {
                return [1]
            },
        },
        maximagescount: {
            type: Number,
            default: 0
        },
    },
    data() {
        return {
            download: Config.api.download,
            list: [],
            unwacth: null,
            count:'0/0',
            isOffline:false,
        }
    },

    created() {
        this.unwacth = this.$watch(
            'dataKey',
            function (val) {
                if (val) {
                    this.getFileList()
                }
            },
            {
                deep: true,
                immediate: true,
            }
        )
    },
    methods: {
        actionsGroup(item){
            if(this.isOffline){
                return [1,2]
            }else{
                return item.uploadFrom?item.uploadFrom.split(','):this.actionsOneGroup
            }
        },
        getFileList(updateDefault = false) {
            // 取消监听
            if (this.unwacth) this.unwacth()
            this.$f7.request.post(
                '/admin/attachmentFormDetail/listUpload',
                {
                    typeCode: this.typeCode,
                    dataKey: this.dataKey,
                    attachCraneType: this.attachCraneType,
                },
                (r) => {
                    r = JSON.parse(r)
                    if (r?.success && r.parameters.fileList) {
                        this.list = r.parameters.fileList
                        this.isOffline=r.parameters.offline
                        this.$emit('onAfterQuery', this.list)
                        if(updateDefault){ // 回显默认的数据
                            this.list.forEach((item,index)=>{
                                this.$refs[`${this.listName}${index + 1}`][0].mapFileList(item.attachments);
                            })
                        }
                        return
                    }
                    this.list = []
                    this.$framework.$f7.dialog.toast('获取文件失败')
                }
            )
        },
        getUploadedCount() {
            let total = 0
            let count = 0
            this.list.forEach((element) => {
                if (element.isMustUpload == 1&&!element.isNil) {
                    total++
                }
            })
            this.list.forEach((element, index) => {
                if (
                    element.isMustUpload == 1 &&
                    this.$refs[`${this.listName}${index + 1}`][0].getUploadedCount() > 0 &&
                    !element.isNil
                ) {
                    count++
                }
            })
            return total == count ? true :false
        },
        checkItem(index,item) {
            this.$emit('check', this.listName, index, this.list)
            this.$emit('checkItem',index,item)
        },
        fileOnSuccess(){
            this.getNum()
        },
        deleteFileAfter(){
            this.getNum()
        },
        getCounts(){
            return new Promise((resolve, reject) => {
                try {
                    let count=0
                    let getCount=0
                    this.list.forEach((element, index) => {
                        if(Array.isArray(this.$refs[`${this.listName}${index + 1}`])){
                            getCount=this.$refs[`${this.listName}${index + 1}`][0].getUploadedCount()
                        }else{
                            getCount=this.$refs[`${this.listName}${index + 1}`].getUploadedCount()
                        }
                        if (getCount > 0 ) {
                            count++
                        }
                    })
                    resolve (`${count}/${this.list.length}`)
                } catch (error) {
                    resolve('0/0')
                }
            })
        },
        getNum(){
            this.$nextTick(async ()=>{
                this.count=await this.getCounts()
                this.$emit('getCount',this.count)
            })
        },
    },
    watch:{
        list:{
            handler(val){
                this.getNum()
            },
            immediate:true,
            deep:true,
        }
    }
}
</script>

<style lang="less" scoped>
@import url('../../assets/css/commonPage.less');

@{deep}.accordion .accordion-item > .item-link > .item-content {
    padding-left: 16px !important;
}

.picList .infoList {
    margin-top: 10px;
}

li {
    &:first-child .content-box {
        margin-top: 0;
    }

    .content-box {
        background: #fff;
        margin-bottom: 0;
        align-items: flex-start;
        box-shadow: none;
        padding: 10px 0;
    }

    .index-box {
        padding: 2px 8px;
        background: rgba(51, 133, 255, 0.1);
        border-radius: 1px;
        font-size: 13px;
        color: #1990ff;
    }

    .right-box {
        margin-left: 10px;
        font-size: 14px;
        color: #333;
        .code{
            display: flex;
            justify-content: space-between;
            align-items: center;
            section{
                display: flex;
                span{
                    flex-shrink: 0;
                    height: 16px;
                    line-height: 16px;
                }
            }
        }
    }

    .checks-box {
        display: flex;
        align-items: flex-start;
        justify-content: space-between;

        .code {
            width: 72%;
        }
    }
}
/deep/.pic_upload {
    padding: 10px 0 !important;
}
.right-box-bottom{
    display: flex;
    
}
.noUpload{
    background: #f7f8fa;
    color: #b7c2d9;
    border: 1px solid #b7c2d9;
    border-radius: 6px;
    border-bottom-left-radius: 0;
    padding: 2px 5px;
    font-size: 12px;
    margin-left: 5px;
}
</style>
